package com.leyou.cart.controller;

import com.leyou.cart.Service.CartService;
import com.leyou.cart.pojo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    //商品添加进购物车
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){
        cartService.addCart(cart);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
    @GetMapping
    public ResponseEntity<List<Cart>> queryCarts(){
        List<Cart> carts = cartService.queryCarts();

        if (carts != null) {
            return ResponseEntity.ok(carts);
        }

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }
    @PutMapping("modify/decrement")
    public ResponseEntity<Void> decrement(@RequestParam("skuId") Long skuId){
        this.cartService.decrement(skuId);
        return ResponseEntity.ok().build();
    }
    @PutMapping("modify/increment")
    public ResponseEntity<Void> increment(@RequestParam("skuId") Long skuId){
        this.cartService.increment(skuId);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("modify/delete/{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId) {
        this.cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }
}
